package pixelwaks.gui.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ResourceBundle;

import javax.swing.JComponent;
import javax.swing.JTextArea;

import pixelwaks.ctrl.ControlFactory;
import pixelwaks.ctrl.impl.ModeCtrl;

import common.i18n.ChangeLanguage;
import common.i18n.IChangeLanguage;

/**
 * Class for creating a mode information panel, which fetches and displays the description 
 * of the current mode in the currently selected language.
 * @author Joakim
 *
 */
public class ModeInfo extends JComponent implements IChangeLanguage {

	private static final long serialVersionUID = 1L;
	
	JTextArea info = new JTextArea();
	
	private int smallRectXPos;
	private int smallRectYPos; 
	private int smallRectWidth;
	private int smallRectHeight;
	private int largeRectXPos;
	private int largeRectYPos;
	private int largeRectWidth;
	private int largeRectHeight;
	private int textAreaXPos;
	private int textAreaYPos;
	private int textAreaWidth;
	private int textAreaHeight;
	private int arcSize;
	
	private String modeName;
	private String modeDescription;
	
	private Font arial12;
	private Font arialBlack16;
	
	/**
	 * Creates a mode information panel and draws it.
	 */
	public ModeInfo() {
		
		smallRectYPos = 4;
		smallRectHeight = 25;
		largeRectYPos = smallRectYPos + smallRectHeight - 5;
		largeRectWidth = 380;
		largeRectHeight = 74;
		textAreaYPos = largeRectYPos + 5;
		textAreaWidth = largeRectWidth - 10;
		textAreaHeight = largeRectHeight - 10;
		arcSize = 10;
		
		ModeCtrl mctrl = (ModeCtrl) ControlFactory.getObserver(ControlFactory.MODE_CTRL);
		modeName = mctrl.getSelectedMode();
		
		modeDescription = ChangeLanguage.currentLanguage.getString(modeName);
		
		ChangeLanguage.addObserver(this);
		info.setEditable(false);
		info.setLineWrap(true);
		info.setWrapStyleWord(true);
		info.setFont(arial12);
		info.setBackground(Color.GRAY);
		info.setForeground(Color.WHITE);
		
		add(info);
		
		setBackground(Color.WHITE);
		setVisible(true);
				
		//fonts
		arial12 = new Font("Arial", Font.PLAIN, 12);
		arialBlack16 = new Font("Arial Black", Font.PLAIN, 16);
	}
	
	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		
		smallRectWidth = g2.getFontMetrics(arialBlack16).stringWidth(modeName) + 10;
		smallRectXPos = this.getWidth()/2 - smallRectWidth/2;
		largeRectXPos = this.getWidth()/2 - largeRectWidth/2;
		textAreaXPos = largeRectXPos + 5;
		
		g2.setColor(Color.GRAY);
		g2.fillRoundRect(smallRectXPos, smallRectYPos, smallRectWidth, smallRectHeight, arcSize, arcSize);
		g2.fillRoundRect(largeRectXPos, largeRectYPos, largeRectWidth, largeRectHeight, arcSize, arcSize);
		g2.setColor(Color.WHITE);
		g2.setFont(arialBlack16);
		g2.drawString(modeName, this.getWidth()/2 - g2.getFontMetrics().stringWidth(modeName)/2, smallRectYPos + 17);
		info.setBounds(textAreaXPos, textAreaYPos, textAreaWidth, textAreaHeight);
		info.setText(modeDescription);
	}

	@Override
	public void setLanguage(ResourceBundle rb) {
		modeDescription = rb.getString(modeName);
		repaint();
	}

}
